package com.example.connecting_dots_android;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.TextView;

public class Settings extends Activity {
	private CheckBox mChechBoxBackgroundMusic;
	private boolean mIsMute = false;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);
		init();
		settings();
	}

	/**
	 * method to initialize class members
	 * */
	private void init() {
		mChechBoxBackgroundMusic = (CheckBox) findViewById(R.id.checkBoxBackgroundMusic);

		mIsMute = Utils.getIsMute(this);

		mChechBoxBackgroundMusic.setChecked(mIsMute);
		mChechBoxBackgroundMusic
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						// TODO Auto-generated method stub
						mIsMute = !mIsMute;
						SharedPreferences mySharedPreferences = getSharedPreferences(
								"mute", Activity.MODE_PRIVATE);
						SharedPreferences.Editor editor = mySharedPreferences
								.edit();
						editor.putBoolean("isMute", mIsMute);
						editor.commit();
						if (mIsMute) {
							MainActivity.mServ.start();
						} else {
							MainActivity.mServ.stop();
						}
					}
				});

		// set listeners to the background images
		setListenersToBackgroundImages();
	}

	/**
	 * method to manage background image and music
	 * */
	private void settings() {
		if (MainActivity.mServ != null && mIsMute
				&& !MainActivity.mServ.isPlaying()) {
			MainActivity.mServ.resume();
		}

		Utils.setBackgroundImage(this, findViewById(R.id.settingsLayout));
	}

	/**
	 * method to set OnCLickListeners on the background images
	 * */
	private void setListenersToBackgroundImages() {
		for (int i = 0; i < Constants.backgroundsIds.length; i++) {
			getBackgroundImageView(i).setOnClickListener(
					new BackgroundImageListener(this, i,
							findViewById(R.id.settingsLayout)));
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.settings, menu);
		return true;
	}

	private ImageView getBackgroundImageView(int id) {
		if (id >= 0 && id < Constants.bgdImages.length)
			return (ImageView) findViewById(Constants.bgdImages[id]);
		else
			return (ImageView) findViewById(Constants.bgdImages[0]);
	}

	@Override
	protected void onPause() {
		super.onPause();
		mIsMute = Utils.getIsMute(this);
		if (MainActivity.mServ != null && mIsMute) {
			MainActivity.mServ.pause();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		mIsMute = Utils.getIsMute(this);
		settings();
	}
}
